package com.yoomiito.app.ui.fragment;

import android.os.Bundle;

import com.google.android.material.appbar.AppBarLayout;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.appcompat.widget.Toolbar;
import android.widget.Toast;

import com.yoomiito.app.Constant;
import com.yoomiito.app.MainActivity;
import com.yoomiito.app.R;
import com.yoomiito.app.adapter.home.nav.NavGridAdapter;
import com.yoomiito.app.base.App;
import com.yoomiito.app.base.BaseLazyFragment;
import com.yoomiito.app.model.bean.GoodsInfo;
import com.yoomiito.app.model.bean.NavInfo;
import com.yoomiito.app.present.home.HomeNavPresenter;
import com.yoomiito.app.ui.search.SearchGoodsAdapter;
import com.yoomiito.app.utils.AllCode;
import com.yoomiito.app.utils.DensityUtil;
import com.yoomiito.app.utils.EventMessage;
import com.yoomiito.app.utils.RouterUtils;
import com.yoomiito.app.widget.SpacingDecoration;
import com.yoomiito.app.widget.TabView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 首页-----其他导航
 */
public class HomeNavFragment extends BaseLazyFragment<HomeNavPresenter> {
    @BindView(R.id.fm_home_nav_appBarLayout)
    AppBarLayout mAppBarLayout;
    @BindView(R.id.fm_home_nav_grid)
    RecyclerView mGridRcy;
    @BindView(R.id.fm_home_nav_rcy)
    RecyclerView mRecyclerView;
    @BindView(R.id.fm_home_nav_tabView)
    TabView mTabView;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.fm_home_nav_refreshLayout)
    SwipeRefreshLayout mSwipeRefreshLayout;

    public String type;
    private String navId;
    private List<NavInfo> mNavInfos = new ArrayList<>();
    private NavGridAdapter mGridAdapter;
    private String key;
    private String sort = "";
    private int hasCoupon = 1;
    private int currentPage = 1;
    private List<GoodsInfo> mGoodsInfos = new ArrayList<>();
    private SearchGoodsAdapter goodsAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.fm_home_nav;
    }

    @Override
    public HomeNavPresenter newP() {
        return new HomeNavPresenter(App.mAppConfig);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        Bundle bundle = getArguments();
        navId = bundle.getString(Constant.ID);
        initBarLayout();
        ((AppCompatActivity) context).setSupportActionBar(mToolbar);
        ((AppCompatActivity) context).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mToolbar.setTitle("");
        initSwipeRefreshLayout();
        initGrid();
        initTabLayout();
        getP().getNav(navId);
    }

    private void initBarLayout() {
        mAppBarLayout.addOnOffsetChangedListener((appBarLayout, verticalOffset) -> {
            if (verticalOffset >= 0) {
                mSwipeRefreshLayout.setEnabled(true);
            } else {
                mSwipeRefreshLayout.setEnabled(false);
            }
        });
    }

    //下拉
    private void initSwipeRefreshLayout() {
        mSwipeRefreshLayout.setOnRefreshListener(() -> {
            currentPage = 1;
            getP().searchGoods(key, sort, hasCoupon, currentPage);
        });
    }

    private void initTabLayout() {
        mTabView.setDefault()
                .setSelectCoupon();
//        mTabView.setOnChangeListener((sort, hasCoupon) -> {
////            this.sort = sort;
//            this.hasCoupon = hasCoupon;
//            currentPage = 1;
//            showLoading();
////            getP().searchGoods(key, sort, hasCoupon, currentPage);
//        });
    }

    private void initGrid() {
        GridLayoutManager manager = new GridLayoutManager(context, 4);
        mGridRcy.setLayoutManager(manager);
        mGridAdapter = new NavGridAdapter(mNavInfos);
        mGridRcy.setAdapter(mGridAdapter);
        mGridAdapter.setOnItemClickListener((adapter, view, position) -> {
            key = mNavInfos.get(position).getName();
            currentPage = 1;
            showLoading();
            getP().searchGoods(key, sort, hasCoupon, currentPage);
        });

        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        goodsAdapter = new SearchGoodsAdapter(mGoodsInfos);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.addItemDecoration(new SpacingDecoration(0, DensityUtil.dp2px(10),false));
        mRecyclerView.setAdapter(goodsAdapter);
        goodsAdapter.setOnLoadMoreListener(() -> {//上拉
            currentPage++;
            getP().searchGoods(key, sort, hasCoupon, currentPage);
        }, mRecyclerView);
        goodsAdapter.setOnItemClickListener((adapter, view, position) -> {
            GoodsInfo info = goodsAdapter.getData().get(position);
            RouterUtils.toGoodsDetail((MainActivity) context, String.valueOf(info.getNum_iid()), AllCode.TaoBao);
        });
    }

    public void setNavData(List<NavInfo> navInfos) {
        mNavInfos.clear();
        mNavInfos.addAll(navInfos);
        mGridAdapter.notifyDataSetChanged();
        key = mNavInfos.get(0).getName();
        getP().searchGoods(key, sort, hasCoupon, currentPage);
    }

    public void setData(List<GoodsInfo> goodsInfos) {
        dismissLoading();
        if (currentPage == 1) {
            mGoodsInfos.clear();
        }
        if (goodsInfos == null || goodsInfos.size() == 0) {
            Toast.makeText(context, "没有更多了", Toast.LENGTH_SHORT).show();
            goodsAdapter.loadMoreEnd(true);
        } else {
            mGoodsInfos.addAll(goodsInfos);
            goodsAdapter.notifyDataSetChanged();
            goodsAdapter.loadMoreComplete();
        }
    }

    public void finishRefresh(){
        goodsAdapter.loadMoreComplete();
    }

    @Override
    public void eventMessage(EventMessage eventMessage) {
        super.eventMessage(eventMessage);
        if (AllCode.LoginOut.equals(eventMessage.getEventType())
                ||AllCode.LoginSuccess.equals(eventMessage.getEventType())){
            currentPage = 1;
            showLoading();
            getP().searchGoods(key, sort, hasCoupon, currentPage);
        }
    }
}
